package com.knoya.chejian.pda.util

import android.Manifest
import android.app.Activity
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.PermissionListener


/**
 * Created by Administrator on 2016/12/2/002.
 * 作者：刘洋
 * 6.0之后获取权限的工具类
 */
class PermissionUtils {
    companion object {
        /**
         * 验证扫描的权限
         */
        fun issaomiao(activity: Activity,permissionListener: PermissionListener): Boolean {
            val pers = arrayOf(android.Manifest.permission.CAMERA)
            isPermisson(activity,permissionListener, pers)
            return false
        }

        /**
         * 验证拍照的权限
         */
        fun isphoto(activity: Activity, permissionListener: PermissionListener) {
            val pers = arrayOf(android.Manifest.permission.CAMERA, android.Manifest.permission.WRITE_EXTERNAL_STORAGE, android.Manifest.permission.READ_EXTERNAL_STORAGE)
            isPermisson(activity, permissionListener, pers)
        }

        /**
         * 下载的权限
         */
        fun isupdate(activity: Activity,permissionListener: PermissionListener) {
            val pers = arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
            isPermisson(activity,permissionListener, pers)
        }
        /**
         * 下载安装apk的权限
         */
        fun isupdateapk(activity: Activity,permissionListener: PermissionListener) {
            val pers = arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE)
            isPermisson(activity,permissionListener, pers)
        }
        fun isPermisson(activity: Activity, permissionListener: PermissionListener, pers: Array<String>) {
            AndPermission.with(activity)
                    .requestCode(100)
                    .permission(*pers)
                    .callback(permissionListener)
                    // rationale作用是：用户拒绝一次权限，再次申请时先征求用户同意，再打开授权对话框；
                    // 这样避免用户勾选不再提示，导致以后无法申请权限。
                    // 你也可以不设置。
//                    .rationale { _, rationale ->
//                        AndPermission.rationaleDialog(activity, rationale).show() }
                    .start()

        }
    }
}
